/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.PanelContent;

/**
 *
 * @author WenYong
 */
@FacesConverter(value = "panelContentConverter", forClass = util.enumeration.PanelContent.class)
public class PanelContentConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value == null || value.length() == 0) {
            return null;
        }

        if (value.equals("CONTENT1")) {
            return PanelContent.CONTENT1;
        } else if (value.equals("CONTENT2")) {
            return PanelContent.CONTENT2;
        } else {
            return PanelContent.CONTENT3;
        }
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null) {
            return null;
        }

        if (value instanceof PanelContent) {
            PanelContent panelContent = (PanelContent) value;

            if (panelContent.equals(PanelContent.CONTENT1)) {
                return "CONTENT1";
            } else if (panelContent.equals(PanelContent.CONTENT2)) {
                return "CONTENT2";
            } else {
                return "CONTENT3";
            }
        } else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.OperatingHours");
        }
    }
}
